/*
 *  Copyright (C) <2009>  Pavel Vakeš <pavel.vakes@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <account_dialog.h>

#include "config_dialog.h"


AccountDialog::AccountDialog(QWidget * parent, ListAccountItem * account_item) : QWidget(parent, Qt::Dialog)
{
	_account_item = account_item;

	setupUi(this);
	setWindowModality(Qt::WindowModal);

	// Check input fields
	connect(numInterval, SIGNAL( valueChanged(int) ), this, SLOT( checkInputFields() ));
	connect(numConnectionTimeout, SIGNAL( valueChanged(int) ), this, SLOT( checkInputFields() ));

	// Change default port
	connect(selProtocol, SIGNAL( currentIndexChanged(int) ), this, SLOT( changeDefaultPort() ));
	connect(checkSSL, SIGNAL( stateChanged(int) ), this, SLOT( changeDefaultPort() ));

	// Connect buttons
	connect(butOK, SIGNAL( clicked() ), parent, SLOT( closeAccountDialog() ));
	connect(butCancel, SIGNAL( clicked() ), this, SLOT( close() ));

	loadAccount();
}

AccountDialog::~AccountDialog()
{
}

void AccountDialog::loadAccount()
{
	Account * account = _account_item->account;

	txtName->setText(account->name);
	numInterval->setValue(account->interval);
	checkAfterRun->setChecked(account->check_after_run);
	selProtocol->setCurrentIndex(selProtocol->findText(account->protocol));
	txtHost->setText(account->host);
	checkSSL->setChecked(account->ssl);
	numPort->setValue(account->port);
	txtUsername->setText(account->username);
	txtPassword->setText(account->password);
	numConnectionTimeout->setValue(account->connection_timeout);
	txtMailClient->setText(account->mail_client);
}

void AccountDialog::saveAccount()
{
	Account * account = _account_item->account;

	account->name = txtName->text();
	account->interval = numInterval->value();
	account->check_after_run = checkAfterRun->isChecked();
	account->protocol = selProtocol->currentText();
	account->host = txtHost->text();
	account->port = numPort->value();
	account->ssl = checkSSL->isChecked();
	account->username = txtUsername->text();
	account->password = txtPassword->text();
	account->connection_timeout = numConnectionTimeout->value();
	account->mail_client = txtMailClient->text();

	_account_item->setText(account->name);
	_account_item->updateStatus();

	if (!_account_item->listWidget()) {
		QListWidget * listAccounts = ((ConfigDialog *) parentWidget())->listAccounts;
		listAccounts->addItem(_account_item);
		listAccounts->setCurrentRow(listAccounts->count()-1);
	}
}

void AccountDialog::checkInputFields()
{
	if (selProtocol->currentIndex() == -1) selProtocol->setCurrentIndex(0);
	if (numConnectionTimeout->value() > numInterval->value()) numConnectionTimeout->setValue(numInterval->value());
}

void AccountDialog::changeDefaultPort() {
	if (selProtocol->currentText() == "IMAP4") {
		numPort->setValue(checkSSL->isChecked() ? 993 : 143);
	}
	else if (selProtocol->currentText() == "POP3") {
		numPort->setValue(checkSSL->isChecked() ? 995 : 110);
	}
}
